package com.mingrisoft.jdbc;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FindProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int currPage = 1;
		if(request.getParameter("page") != null){
			currPage = Integer.parseInt(request.getParameter("page"));
		}
		ProductDao pd = new ProductDao();
		List<ProductEntity> list = pd.find(currPage);
		request.setAttribute("list", list);
		int pages; // 总页数
		int count = pd.findCount();
		if(count % ProductEntity.PAGE_SIZE == 0){
			pages = count/ProductEntity.PAGE_SIZE;
		}else{
			pages = count/ProductEntity.PAGE_SIZE + 1;
		}
		StringBuffer sb = new StringBuffer();
		//构建分页条
		for(int i=1; i<=pages; i++){
			if(i == currPage){
				sb.append("[" + i + "]");
			}else{
				sb.append("<a href='FindProductServlet?page=" + i + "'>" + i + "</a>");
			}
			sb.append(" ");
		}
		request.setAttribute("bar", sb.toString());
		request.getRequestDispatcher("sl/10/8/product_list.jsp").forward(request, response);
	}

}

